#include"XYXKC_I2C.h"
XYXKC_I2C::XYXKC_I2C(){}

/**
* @brief 设备以主机的形式加入
*/
void XYXKC_I2C::init(){
    _wire->begin();
}

/**
* @brief 初始化 设置从机的地址
*/
void XYXKC_I2C::begin()
{
    _i2caddr = I2C_STC8H8K64U_ADDR;
    _wire = &Wire;
    init();
}
/**
 *  @brief  发送数据
 *  @param  role 用途 0控制继电器 1读数字引脚 2读继电器状态
 *  @param  sentDate 发送的数据 senDate 范围0~256 
 */

void XYXKC_I2C::_writeByte(uint8_t role,uint8_t sentData){
    _wire->beginTransmission(_i2caddr);
    //由于发送的第一字节数据不会被读到 所以一开始随便发送一个字节
    #if ARDUINO >= 100
    _wire->write(0);
    _wire->write(role);
    _wire->write(sentData);
    #else
    _wire->send(0);
    _wire->send(role);
    _wire->send(sentData);
    #endif
    _wire->endTransmission();
    //delay(10);
}

/**
 *  @brief  获取数字输出引脚状态
 */
void XYXKC_I2C::readRelay()
{
    //发送读取停止，避免发送的数据被读出去，导致发送失败 
    _wire->requestFrom(_i2caddr,(uint8_t)1,true);
    _writeByte(1, 0);
    int i = 0;
    _wire->requestFrom(_i2caddr, (uint8_t)15);    
    while (_wire->available()> 0) 
    { 
        if(i == 0){

            #if ARDUINO >= 100
                _wire->read();
            #else
                _wire->receive();
            #endif 
        }else{
            #if ARDUINO >= 100
                relayDate[i-1] = _wire->read();
            #else
                relayDate[i-1] = _wire->receive();
            #endif
        }
        i++;    
    }
}

/**
 *  @brief  获取数字引脚数据
 *  @param  date 数据存放地址
 */

void XYXKC_I2C::readIO(uint8_t *date)
{
    //发送读取停止，避免发送的数据被读出去，导致发送失败 
    _wire->requestFrom(_i2caddr,(uint8_t)1,true);
    _writeByte(2, 0);
    int i = 0;
    _wire->requestFrom(_i2caddr, (uint8_t)17);    
    while (_wire->available()> 0) 
    { 
        if(i == 0){

            #if ARDUINO >= 100
                _wire->read();
            #else
                _wire->receive();
            #endif 
        }else{
            #if ARDUINO >= 100
                date[i-1] = _wire->read();
            #else
                date[i-1] = _wire->receive();
            #endif
        }

        i++;    
    }
}





/**
 *  @brief  数字控制
 *  @param  sentDate 发送的数据 senDate 范围0~256 
 */
void XYXKC_I2C::write(uint8_t sentDate)
{
    _writeByte(0, sentDate);
}

/**
*  @brief 获取数字引脚状态字符串
*/
String XYXKC_I2C::readString(uint8_t len){
    String stringData ="";
    if(len == 2){
        for (int i = 8; i < 16; i++) {
            stringData += String(date[i]);
        } 
        return stringData;
    }else if(len == 1) {
        for (int i = 0; i < 8; i++) {
           stringData += String(date[i]);
        } 
        return stringData;
    }else{
        for (int i = 0; i < 16; i++) {
            stringData += String(date[i]);
        }
        return stringData;
    }

}


/**
*  @brief 获取数字输出引脚状态字符串
*/
String XYXKC_I2C::readRelayString(){
        String stringData = "";
        for (int i = 0; i < 14; i++) {
            stringData  += String(relayDate[i]);
        }  
        return stringData;
}

/**
*  @brief 设置舵机角度
*  @param  io 舵机引脚 20~23 
*  @param  angle 转动的角度 0~180 
*/
void XYXKC_I2C::servoWrite(uint8_t io,uint8_t angle){

    _wire->beginTransmission(_i2caddr);
        //由于发送的第一字节数据不会被读到 所以一开始随便发送一个字节
        #if ARDUINO >= 100
        _wire->write(0);
        _wire->write(3);
        _wire->write(io);
        _wire->write(angle);
        #else
        _wire->send(0);
        _wire->send(3);
        _wire->send(io);
        _wire->send(angle);
    #endif
    _wire->endTransmission();
}


/**
*  @brief 设置引脚pwm输出
*  @param  io 引脚 20~23 
*  @param  angle 输出值 0~100 
*/
void XYXKC_I2C::pwmWrite(uint8_t io,uint8_t angle){

    _wire->beginTransmission(_i2caddr);
        //由于发送的第一字节数据不会被读到 所以一开始随便发送一个字节
        #if ARDUINO >= 100
        _wire->write(0);
        _wire->write(4);
        _wire->write(io);
        _wire->write(angle);
        #else
        _wire->send(0);
        _wire->send(4);
        _wire->send(io);
        _wire->send(angle);
    #endif
    _wire->endTransmission();
}